package 
{
    import flash.events.*;
    import flash.display.*;
    import flash.media.*;
    import flash.net.*;
	import flash.geom.*;
	import GradientStatus;
	
	// Load the TweenMax Class Libraries
	import gs.*; 
	import gs.easing.*;
	import gs.plugins.*;
	TweenPlugin.activate([ColorTransformPlugin]);
	
	// Import the Tweener Libraries
	import caurina.transitions.Tweener;
	import caurina.transitions.properties.ColorShortcuts;

    public class ShiftingGradient extends MovieClip {
		
		private var myStage:Object;
		private var changeSpeed:Number = 5;
		private var gradientStatus:GradientStatus;

        public function ShiftingGradient(_stage:Object, _gradientStatus:GradientStatus) {
	
			myStage = _stage;
			gradientStatus = _gradientStatus;
			
			ColorShortcuts.init();

			// First, create a Shape with a gradient fill.
			var shp:Shape = new Shape();
			var mtx:Matrix = new Matrix();  // This matrix is needed for Shape.beginGradientFill()
			mtx.createGradientBox( myStage.stageWidth, myStage.stageHeight, Math.PI/2 );
			with( shp.graphics ) {
			    beginGradientFill( GradientType.LINEAR, [0x000000,0xffffff], [1,1], [0,255], mtx );
			    drawRect( 0, 0, myStage.stageWidth, myStage.stageHeight );
			    endFill();
			}

			// Draw the Shape inside some BitmapData.
			var bData:BitmapData = new BitmapData( myStage.stageWidth, myStage.stageHeight, false, 0x000000 );
			bData.draw( shp );

			// Create a Bitmap to display the BitmapData, and add it to the stage.
			var bmp:Bitmap = new Bitmap( bData );
			addChild( bmp );

			// For testing purposes: Set up a mouse click listener. When the user clicks, tween to new colors.
			myStage.addEventListener( MouseEvent.CLICK, onClick );

			function onClick( e:MouseEvent ) :void {
			    // Choose two colors at random
			    var c1:int = 0xffffff * Math.random();
			    var c2:int = 0xffffff * Math.random();
			    trace("Now tweening to:", c1.toString(16), c2.toString(16) );
			    colorChange( c1, c2 );
			}

			// This function supports tweening the gradient to ANY TWO COLORS.
			// If you just want to tint a gradient, then you can use less scary code,
			// but you didn't specify that ;)
			function colorChange( c1:uint, c2:uint ) :void {
				
				// Alert gradientStatus that we are starting to play
				gradientStatus.changeText("PLAYING");
				
			    // Split the incoming color uint's into RGB values, ranging from 0..255
			    var o1:Object = {
			        r: (c1 & 0xff0000) >> 16,
			        g: (c1 & 0x00ff00) >> 8,
			        b:  c1 & 0x0000ff
			    };
			    var o2:Object = {
			        r: (c2 & 0xff0000) >> 16,
			        g: (c2 & 0x00ff00) >> 8,
			        b:  c2 & 0x0000ff
			    };

			    // Start the tween.
			    // Tweener.addTween( bmp, {_colorTransform:ct, time:1.0, transition:"easeOutQuad"} ); // Tweener version of the transform tween
				TweenMax.to(
					bmp, // Object to tween
					changeSpeed, // Duration of the Tween
					{colorTransform:
						{
						redMultiplier: (o2.r-o1.r)/255.0,
						greenMultiplier: (o2.g-o1.g)/255.0, 
						blueMultiplier: (o2.b-o1.b)/255.0,  
						alphaMultiplier: 1,
						redOffset: o1.r,
						greenOffset: o1.g,
						blueOffset: o1.b,
						alphaOffset: 0
						},
					ease:Quad.easeOut,
					onComplete: gradientDoneAlert
					}
				);
				
				function gradientDoneAlert ():void
				{
					gradientStatus.changeText("STOPPED");
				}
			}
        }
    }
}
